package com.dhnsoft.boot3.redis.controller;

import com.dhnsoft.boot3.redis.entity.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author dhn
 * @version 1.0
 * @date 2023-09-11 12:51
 * @Desc
 */
@RestController
public class RedisController {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("count")
    public String redisTest(){
        Long hello = stringRedisTemplate.opsForValue().increment("hello");
        return "访问了【"+hello+"】次";
    }

    @GetMapping("/person/save")
    public String savePerson(){
        Person person = new Person(1L, "代浩楠", 24, new Date());
        redisTemplate.opsForValue().set("person",person);
        return "ok";
    }

    @GetMapping("/person/get")
    public Person getPerson(){
        Person person = (Person) redisTemplate.opsForValue().get("person");
        return person;
    }
}
